<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="学号">
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select allowClear v-model="queryParam.xslb">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yxList.length == 1" allowClear v-model="queryParam.pydw" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="专业" >
                <a-select allowClear v-model="queryParam.zy" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zyList" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.nj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
<!--            <a-col :md="6" :sm="12">
              <a-form-item label="预答辩状态">
                <a-select allowClear v-model="queryParam.ydbzt">
                  &lt;!&ndash; <a-select-option v-for="i in TYPE0136" :key="i.id" :value="i.code">{{ i.name }}</a-select-option> &ndash;&gt;
                  &lt;!&ndash; <a-select-option value="01">未提交</a-select-option>
                  <a-select-option value="02">已提交</a-select-option>
                  <a-select-option value="03">导师审核通过</a-select-option> &ndash;&gt;
                  <a-select-option value="07">院系审核通过</a-select-option>
                  <a-select-option value="03">导师审核通过</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>-->
          </template>
          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button  type="primary" @click="exportShTpl">下载预答辩审核列表</a-button>
            <a-upload style="margin-left: 8px;" @change='handleImport' :fileList="fileList" :beforeUpload="beforeUpload" :showUploadList='false'>
              <a-button type="primary" :loading="uploading">{{uploading ? 'Uploading' : '导入' }}</a-button>
            </a-upload>
              <a-button @click="queryParam = {};$refs.table.refresh()" style="margin-left: 8px;">重置</a-button>

            <!-- <a-button type="primary" style="margin:0 10px">删除</a-button> -->
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="init"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1800 }"
        :alert="{show: true}"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="id">
          <a @click="showCheck(id)">审核</a>
        </template>
      </s-table>
    </a-card>

    <defended-modal
      ref="check"
      :visible="visible"
      :isCheck="true"
      @refresh="$refs.table.refresh(false)"
      @cancel="visible = false"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import defendedModal from '../components/defendedModal'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '所属院系',
          dataIndex: 'dwmc',
          width: 100
        },
        {
          title: '专业',
          dataIndex: 'zymc'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc',
          width: 150
        },
        {
          title: '导师姓名',
          dataIndex: 'dsxm',
          width: 100
        },
        {
          title: '年级',
          dataIndex: 'sznj',
          width: 80
        },
        {
          title: '论文题目',
          dataIndex: 'lwtm'
        },
        {
          title: '课题来源',
          dataIndex: 'tmly'
        },
        {
          title: '状态',
          dataIndex: 'ydbxxztmc',
          width: 180
        },
        {
          title: '创建时间',
          dataIndex: 'createDate',
          width: 200
        },
        {
          title: '操作',
          dataIndex: 'id',
          width: '80px',
          scopedSlots: { customRender: 'operation' },
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.defendedCheckList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yxList: [], // 院系
      },
      visible: false,
      init: false,
      uploading: false,
      fileList: [],
    }
  },
  components: {STable, defendedModal},
  computed: {
    ...mapState({
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0136: state => state.app['TYPE0136']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0136')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'pydw', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.pydw)
        }
        this.$set(this.queryParam, 'nj', res[1].currentGrade)
        this.init = true
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zy', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    showCheck (id) {
      this.visible = true
      this.$api.cultivate.defendedDetail(id).then(res => {
        this.$refs.check.defendedInfo = res.data
      })
    },

    exportShTpl(){
      this.$export('foster/ydbxx/expExcelTpl',this.queryParam)
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file]
      return false
    },
    handleImport(data) {
      const {
        fileList
      } = this
      const formData = new FormData()
      formData.append('file', data.file)
      this.uploading = true
      this.$upload('foster/ydbxx/impExcelTpl', formData).then(res => {
        this.uploading = false
        this.$message.success(res.msg)
      }).finally(res =>{
        this.uploading = false
      })
    },
  }
}
</script>

<style>

</style>
